In [1]:
%matplotlib inline

Advanced Loops

01. Numbers 1...N with Step 3

Problem: Write a program that enters a number and prints the numbers from 1 to n through 3 (step


In [2]:
n = int(input())

for i in range(1,n + 1, 3):
    print(i)


5
1
4

02. Numbers N...1

Problem: Write a program that sets a positive integer n and prints the numbers from n to 1 in reverse order (from the largest to the smallest).


In [5]:
n = int(input())


for i in range(n, 0, -1):
    print(i)


5
5
4
3
2
1

03. Powers of Two

Problem: Write a program that reads from the console an integer n and prints the numbers from 1 to 2n.


In [7]:
n = int(input())

for i in range(0, n + 1, 1):

    print(pow(2,i))


7
1
2
4
8
16
32
64
128

04. Even Powers of 2

Problem: Write a program that enters n and prints the even degrees at 2 ≤ 2n: 20, 22, 24, 28, ..., 2n.


In [9]:
n = int(input())

for i in range(0, n + 1, 1):

    if i % 2 == 0:
        print(pow(2,i))


5
1
4
16

05. Sequence 2k+1

Problem: Write a program that enters a number n and prints all numbers ≤ n of the row: 1, 3, 7, 15, 31, .... Each subsequent number is calculated as the previous number * 2 + 1.


In [10]:
n = int(input())

result = 1

while result <= n:

    print(result)
    result = result * 2 + 1


5
1
3

06.Number in Range [1...100]

Problem: Write a program that introduces a positive integer n in the range [1 ... 100]. When entering a number outside the specified range, an error message is printed and the user is prompted to enter a new number.


In [12]:
n = int(input('Еnter a number in the range [1...100]: '))

while 1 > n or n > 100:
    print("Invalid number!")
    n = int(input('Еnter a number in the range [1...100]: '))



print("The number is: "+ str(n))


Еnter a number in the range [1...100]: 5
The number is: 5

07.Greatest Common Divisor (CGD)

Problem: Write a program that introduces two integer positive integers a and b and calculates and prints their largest common divisor (NOD).


In [14]:
a = int(input())
b = int(input())

while b != 0:

    save = a % b
    a = b
    b = save

print(a)


5
4
1

08.Factorial

Problem: Write a program that enters an integer n (1 ≤ n ≤ 12) and calculates and prints n! = 1 2 ... * n (n factorial).


In [15]:
n = int(input())

result = 1

for i in range(1, n + 1):

    result *= i


print(result)


5
120

09. Sum Digits

Problem: Write a program that enters an integer num and prints the sum of its numbers.


In [16]:
n = input()

sum = 0

for i in n:

    sum += int(i)

print(sum)


5
5

10.Check Prime

Problem: Write a program that enters an integer n and checks whether it is a simple number (whether it is divided by itself and per unit). Print "Prime" or "Not prime".


In [17]:
import  math


n = int(input())

is_prime = True

if n < 2:
    print('Not Prime')

else:
    end = int(math.sqrt(n))

    for i in range(2, end + 1):

        if n / i == n//i:
          is_prime = False


if is_prime:
    print('Prime')
else:
    print('Not Prime')


5
Prime

11.Enter Even Number

Problem: Write a program that introduces an even number. If the user has entered the wrong number, a new one must be entered.


In [18]:
n = float(input('Enter even number: '))

while n % 2 != 0:
    print('Invalid number!')
    n = int(input('Enter even number: '))

n = print('Even number entered: ' + str(n))


Enter even number: 5
Invalid number!
Enter even number: 4
Even number entered: 4

12.Fibonacci

Problem: Write a program that enters an integer and calculates the Fibonacci nth number. The zero figure of Fibonacci is 1, the first one is also 1, and the next is the sum of the previous two.


In [19]:
n = int(input())

a = 1
b = 2

for i in range(1, n):
    new_b = a + b
    a = b
    b = new_b


print(a)


5
8